#include <math.h>
#include <matrix.h>
#include <mex.h>

extern "C"
void training_model(float *features, float *labels, float **alpha, int nObs, int nDim, float c, float eps, float tol);

//simple MEX wrapper to call our SVM routine in MATLAB
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    //init some vars
    const mwSize *dims;
    int nObs, nDim;
    float *features, *labels, *alpha;
    features = mxGetPr(prhs[0]);
    labels = mxGetPr(prhs[1]);
    c = mxGetPr(prhs[2]);
    eps = mxGetPr(prhs[3]);
    tol = mxGetPr(prhs[4]);

    //get dimensions
    dims = mxGetDimensions(prhs[0]);
    nObs = (int)dims[1]; nDim = (int)dims[0];

    //create results array and associate it to the output pointer
    plhs[0] = mxCreateDoubleMatrix(nObs,1,mxREAL);
    alpha = mxGetPr(plhs[0]);

    //print out the first two rows of feature set
    for (int j=0; j<2; j++) {
        for (int i=0; i<nDim; i++) {
            mexPrintf("features[%d][%d] = %f\n",j,i,features[j*nDim+i]);
        }
    }

    //make call to main training routine
    //should update alpha to have the correct lagrangian weights
    svm_training(features, labels, alpha, nObs, nDim, c, eps, tol); 

    return;
}
